package com.coocaa.ops.admin.v1.todotask.service.impl;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageInfoBizImpl;
import com.coocaa.ops.admin.v1.hvi.service.HistoryVersionInfoServiceImpl;
import com.coocaa.ops.admin.v1.panel.biz.impl.PanelInfoBizImpl;
import com.coocaa.ops.admin.v1.policygroup.biz.impl.PolicyGroupInfoBizImpl;
import com.coocaa.ops.admin.v1.tab.biz.impl.TabInfoBizImpl;
import com.coocaa.ops.admin.v1.todotask.model.AuditTaskInfoModel;
import com.coocaa.ops.admin.v1.todotask.provider.IDraftTaskQueryProvider;
import com.coocaa.ops.admin.v1.todotask.service.IDraftService;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.PanelInfoEntity;
import com.coocaa.ops.common.core.biz.policygroup.entity.PolicyGroupInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/12/7.
 * @description
 */
public class DraftServiceImpl implements IDraftService {
    private static CommonLanguageProperties commonLanguageProperties;
    @Setter
    private Map<String, IDraftTaskQueryProvider> draftTaskQueryProviderMap;

    @Override
    public Map listDraftPageResult(AuditTaskInfoModel model, Integer userId) {
        IDraftTaskQueryProvider draftTaskQueryProvider = this.getDraftTaskQueryProvider(model.getResourceType());
        PageResultModel pageResultModel = draftTaskQueryProvider.queryDraftPageResult(model, userId);
        return R.ok(pageResultModel);
    }

    @Override
    public int getDraftTotalCount(Integer userId) {
        Map<String, Object> params = InstanceUtil.newHashMap(2);
        params.put("userId", userId);
        params.put("queryIsDraft", 1);
        int panelDraftCount = this.getDraftCountByType(params, ResourceTypeEnum.PANEL.getValue());
        int tabDraftCount = this.getDraftCountByType(params, ResourceTypeEnum.TAB.getValue());
        int homeDraftCount = this.getDraftCountByType(params, ResourceTypeEnum.HOMEPAGE.getValue());
        int policyDraftCount = this.getDraftCountByType(params, ResourceTypeEnum.POLICY_GROUP.getValue());
        return panelDraftCount + tabDraftCount + homeDraftCount + policyDraftCount;
    }

    private int getDraftCountByType(Map<String, Object> params, String type) {
        IDraftTaskQueryProvider draftTaskQueryProvider = this.getDraftTaskQueryProvider(type);
        int draftCount = draftTaskQueryProvider.queryDraftCount(params);
        return draftCount;
    }


    private IDraftTaskQueryProvider getDraftTaskQueryProvider(String type) {
        IDraftTaskQueryProvider draftTaskQueryProvider = draftTaskQueryProviderMap.get(type);
        Assert.notNull(draftTaskQueryProvider, "The implementation class for the corresponding resource "+commonLanguageProperties.getNotNull()+"type=" + type);
        return draftTaskQueryProvider;
    }
    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        DraftServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

}
